<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="configList">
      <el-table-column label="参数名称" align="center" prop="configName" :show-overflow-tooltip="true" />
      <el-table-column label="参数键名" align="center" prop="configKey" :show-overflow-tooltip="true" />
      <el-table-column label="参数键值" align="center" prop="configValue" />
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-switch
            v-model="value"
            active-text="开"
            inactive-text="关"
            @change="handleAuditOnOff(scope.row)">
          </el-switch>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import {
  listConfig,
  getConfig,
  delConfig,
  addConfig,
  updateConfig,
  refreshCache,
  getConfigKey,
} from "@/api/system/config";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      //-----------------
      value: true,
      //-----------------
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        configName: undefined,
        configKey: 'gx.audit.auditOnOff',
        configType: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        configName: [
          { required: true, message: "参数名称不能为空", trigger: "blur" }
        ],
        configKey: [
          { required: true, message: "参数键名不能为空", trigger: "blur" }
        ],
        configValue: [
          { required: true, message: "参数键值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getAuditOnOffKey();
    this.getList();
  },
  methods: {
    getAuditOnOffKey() {
      const _this = this;
      getConfigKey("gx.audit.auditOnOff").then(res => {
        let key = res.msg;
        if (key == "false") {
          _this.value = false;
          console.log(_this.value)
        } else {
          _this.value = true;
        }
      });
    },
    handleAuditOnOff(row) {
      let configId = row.configId;
      let _this = this;
      let value = _this.value;
      getConfig(configId).then(response => {
        _this.form = response.data;
        if (value) {
          _this.form.configValue = "true";
        } else {
          _this.form.configValue = "false";
        }
      });
      this.$confirm('你确定要'+ (value == true ? '打开' : '关闭')+'审核功能?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        updateConfig(_this.form).then(response => {
          this.$modal.msgSuccess("操作成功");
          location.reload();
        });
      }).catch(() => {
        _this.value = !value;
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listConfig(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.configList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
  }
};
</script>
